//
//  YRNKUtil.swift
//  YoruNeko
//
//  Created by liphone on 2021/6/11.
//

import Foundation
import UIKit

class YRNKUtil{
    
    static func getSuitableFileSizeString(size:Int64)->String{
        var unit:String
        if(size<1024){
            unit="B";
        }
        else if(size>=1024&&size<1024*1024){
            unit="KB";
        }
        else if(size>=1024*1024&&size<1024*1024*1024){
            unit="MB";
        }
        else{
            unit="GB";
        }
        return byteCountDescriptionConvert_l2s(by: unit, l: size, savePoint: 1)+unit
    }
    
    static func nowNS()->NSDate{
        return NSDate()
    }
    
    static func now() -> Date{
        return Date()
    }
    
    private static func byteCountDescriptionConvert_l2s(by unitKey:String, l:Int64, savePoint:Int)->String{
        let formatter=NumberFormatter();
        formatter.maximumFractionDigits=savePoint
        var retStr=""
        
        switch unitKey{
        case "MB":
            let size_d = Double(l)/Double(pow(Float(1024), Float(2)))
            retStr.append(formatter.string(from: NSNumber(value: size_d)) ?? "")
        case "KB":
            let size_d = Double(l)/Double(1024)
            retStr.append(formatter.string(from: NSNumber(value: size_d)) ?? "")
        case "GB":
            let size_d = Double(l)/Double(pow(Float(1024), Float(3)))
            retStr.append(formatter.string(from: NSNumber(value: size_d)) ?? "")
        default:
            retStr.append(l.description)
        }
        
        
        return retStr
    }
    
    
    // MARK: - private method
    // MARK: 两个日期的间隔
    private static func daysBetweenDate(toDate: Date) -> Int {
        let components = Calendar.current.dateComponents([.day], from: Date(), to: toDate)
        return abs(components.day!)
    }
    
    // MARK: 日期对应当周的周几. 周一为开始, 周天为结束
    private static func dayForWeekAtIndex() -> Int {
        let components = Calendar.current.dateComponents([.weekday], from: Date())
        
        return (components.weekday! - 1) == 0 ? 7 : (components.weekday! - 1)
    }
    
    // MARK: - public method
    // MARK: 判断是否为同一周
    static func isSameWeek(date: Date) -> Bool {
        let differ = self.daysBetweenDate(toDate: date)
        // 判断哪一个日期更早
        let compareResult = Calendar.current.compare(Date(), to: date, toGranularity: Calendar.Component.day)
        
        // 获取更早的日期
        var earlyDate: Date
        if compareResult == ComparisonResult.orderedAscending {
            earlyDate = Date()
        }else {
            earlyDate = date
        }
        print(earlyDate)
        
        let indexOfWeek = dayForWeekAtIndex()
        let result = differ + indexOfWeek
        
        return result > 7 ? false : true
    }
    
    static func isSameDay(date: Date) -> Bool {
        let diff = self.daysBetweenDate(toDate: date)
        if diff == 0 {
            return true
        }else{
            return false
        }
    }
    /**
     str: 给定字符序列
     sub_str: 可能在str中存在的序列，
     ignoreCase: 比对字符是否忽略大小写
     */
    static func isSubSequence(_ str:String, sub_str:String,ignoreCase:Bool = false)->Bool{
        if str.isEmpty || sub_str.isEmpty{
            return false
        }
        
        var isSubSequence:Bool = true;
        
        var istart = str.startIndex
        let iend = str.index(before: str.endIndex)
        
        var c = sub_str.first!
        var c_i = sub_str.startIndex
        
        while istart < iend {
            if let index = str[istart...iend].firstIndex(where: {
                ch in
                if ignoreCase {
                    if ch.isLetter{
                        return ch.uppercased().first == c.uppercased().first
                    }
                    return ch == c
                }
                else{
                    return ch == c
                }
            }){
                istart = str.index(after: index)
                
                c_i = sub_str.index(after: c_i)
                if c_i != sub_str.endIndex{
                    c = sub_str[c_i]
                }
                else{
                    break
                }
                
            }
            else{
                isSubSequence = false
                break
            }
        }
        
        
        return isSubSequence
    }

    
    


}

/**
 一些全局函数
 */
func YRNKLog(_ tag: String, msg: String){
    print("[\(tag)]:\(msg)\n")
}

func YRNKLog(_ tag:String, item:Any){
    print("[\(tag)]:",item,"\n")
}

func YRNKDegree2Radian(degree:CGFloat)->CGFloat{
    return (CGFloat.pi/CGFloat.init(180))*degree
}
